/*
 * @(#)XmlAdapter.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.xmlConvert;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * XmlAdapter
 * 
 * @author BIS 2012-12-24
 */
public class XmlAdapter
{

    /**
     * getBuilder
     * @Description:
     * @return
     * @throws Exception
     */
    public static DocumentBuilder getBuilder() throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder;
    }

    /**
     * load
     * @Description:
     * @param path
     * @return
     * @throws Exception
     */
    public static Document load(String path) throws Exception
    {
        return getBuilder().parse(path);
    }

    /**
     * getNewDocument
     * @Description:
     * @return
     * @throws Exception
     */
    public static Document getNewDocument() throws Exception
    {
        return getBuilder().newDocument();

    }

    /**
     * load
     * @Description:
     * @param xmlFile
     * @return
     * @throws Exception
     */
    public static Document load(File xmlFile) throws Exception
    {
        return getBuilder().parse(xmlFile);
    }

    /**
     * loadXML
     * @Description:
     * @param xml
     * @return
     * @throws Exception
     */
    public static Document loadXML(String xml) throws Exception
    {
        if (xml == null || xml.equals(""))
        {
            return getNewDocument();
        }
        return getBuilder().parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    }

    /**
     * getText
     * @Description:
     * @param ele
     * @return
     */
    public static String getText(Element ele)
    {
        NodeList nodelist = ele.getChildNodes();
        if (nodelist.getLength() > 0)
        {
            return nodelist.item(0).getNodeValue();
        }
        else
        {
            return "";
        }
    }

    /**
     * writeXmlToOutputStream
     * @Description:
     * @param doc
     * @param out
     * @throws Exception
     */
    public static void writeXmlToOutputStream(Document doc, OutputStream out) throws Exception
    {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        t.transform(source, result);
    }

    /**
     * writeXmlToResponse
     * @Description:
     * @param doc
     * @param response
     * @throws Exception
     */
    public static void writeXmlToResponse(Document doc, HttpServletResponse response) throws Exception
    {
        response.setContentType("text/xml");
        writeXmlToOutputStream(doc, response.getOutputStream());
    }

}
